import DateUtil from "../../engine/DateUtil";
import { EngineConfig } from "../../engine/EngineConfig";
import LocalStorage from "../../engine/LocalStorage";
import { GameConfig } from "../config/GameConfig";

const Local_Key: string = "AccountModel"
export class AccountModel {

    public static instance: AccountModel = new AccountModel();

    public account_type: number = GameConfig.Account_Type.Wechat
    public account: string = ""
    public password: string = ""
    public token: string = ""
    public nickname: string = ""
    public sex: number = 1;
    public headimgurl: string = ""
    public lastLoginTime: number = 0;

    public init() {
        let str: string = LocalStorage.getItem(Local_Key)
        if (str) {
            try {
                let data: object = JSON.parse(str)
                AccountModel.instance.lastLoginTime = data["lastLoginTime"]
                if (DateUtil.now() - AccountModel.instance.lastLoginTime <= EngineConfig.OneDay) {
                    this.password = data["password"]
                    this.json2Model(data)
                }
            } catch (error) {

            }
        }
    }

    public json2Model(data: object) {
        this.account = data["account"]
        this.account_type = data["account_type"]
        this.token = data["token"]
        this.nickname = data["nickname"]
        this.sex = data["sex"]
        this.headimgurl = data["headimgurl"]
        // Logger.log("AccountModel=json2Model==", Accou¸ntModel.instance)
        this.save()
    }

    public save() {
        this.lastLoginTime = DateUtil.now()
        // Logger.log("AccountModel=save==", AccountModel.instance)
        LocalStorage.setItem(Local_Key, JSON.stringify(this))
    }



}